/**
 *
 * http配置
 *
 */
// 引入axios以及element ui中的loading和message组件
import axios from 'axios'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { serialize } from '@/util/util'
axios.defaults.timeout = 30000
//跨域请求，允许保存cookie
axios.defaults.withCredentials = true
NProgress.configure({ showSpinner: false }) // NProgress Configuration
//HTTPrequest拦截
axios.interceptors.request.use(
  config => {
    NProgress.start() // start progress bar
    if (config.method === 'post' && config.headers.serialize) {
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
      config.data = serialize(config.data)
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
//HTTPresponse拦截
axios.interceptors.response.use(
  res => {
    NProgress.done()
    return res
  },
  error => {
    NProgress.done()
    return Promise.reject(new Error(error))
  }
)

export default axios
